﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Media.Animation;

using SBPweb.PhotoGallery.Common;
using SBPweb.PhotoGallery.Client.Logic;
using SBPweb.PhotoGallery.Client.Utils;

namespace SBPweb.PhotoGallery.Client
{

    public partial class ViewToolBar : ButtonPresenterControl
    {

        #region Dependency property definitions

        public static readonly DependencyProperty IsAutoAdvanceActiveProperty = DependencyProperty.Register("IsAutoAdvanceActive", typeof(bool), typeof(ViewToolBar), new PropertyMetadata(false));

        #endregion

        #region Private fields

        private AdvancingGallery gallery;
        private bool isFullScreen;

        #endregion

        #region Constructor logic

        public ViewToolBar()
        {
            InitializeComponent();
            InitializeProperties();
            InitializeHandlers();
        }

        protected override void InitializeProperties()
        {
            base.InitializeProperties();
            IsAutoAdvanceActive = (bool)this.GetValue(IsAutoAdvanceActiveProperty);
        }

        #endregion

        #region Property accessors

        public AdvancingGallery Gallery
        {
            get
            {
                return gallery;
            }
            set
            {
                FinalizeGallery();
                gallery = value;
                InitializeGallery();
            }
        }

        public override Brush ButtonForeground
        {
            get
            {
                return base.ButtonForeground;
            }
            set
            {
                base.ButtonForeground = value;
                buttonFullScreen.BorderBrush = value;
                buttonFullScreenPath.Stroke = value;
                buttonNext.BorderBrush = value;
                buttonLast.BorderBrush = value;
                buttonPause.BorderBrush = value;
                buttonPauseStroke.Stroke = value;
                buttonPlay.BorderBrush = value;
                buttonPrev.BorderBrush = value;
                buttonFirst.BorderBrush = value;
                buttonSave.BorderBrush = value;
                buttonSaveFill.Fill = value;
                buttonSaveStroke.Stroke = value;
                tbTitle.Foreground = value;
            }
        }

        public override Brush ButtonAlternateForeground
        {
            get
            {
                return base.ButtonAlternateForeground;
            }
            set
            {
                base.ButtonAlternateForeground = value;
                buttonPlayFill.Fill = value;
                buttonPlayFill.Stroke = value;
                buttonPlayStroke.Stroke = value;
            }
        }

        public override Brush ButtonBackground
        {
            get
            {
                return base.ButtonBackground;
            }
            set
            {
                base.ButtonBackground = value;
                buttonFullScreen.Background = value;
                buttonNext.Background = value;
                buttonLast.Background = value;
                buttonPause.Background = value;
                buttonPlay.Background = value;
                buttonPrev.Background = value;
                buttonFirst.Background = value;
                buttonSave.Background = value;
            }
        }

        public bool IsAutoAdvanceActive
        {
            get
            {
                return (bool)this.GetValue(IsAutoAdvanceActiveProperty);
            }
            set
            {
                this.SetValue(IsAutoAdvanceActiveProperty, value);
                buttonPause.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
                buttonPlay.Visibility = value ? Visibility.Collapsed : Visibility.Visible;
                if (value)
                {
                    buttonPauseStoryboard.Begin();
                }
                else
                {
                    buttonPauseStoryboard.Stop();
                }
                if (gallery is AutoAdvancingGallery)
                {
                    (gallery as AutoAdvancingGallery).IsAutoAdvanceActive = value;
                }
            }
        }

        #endregion

        #region Methods

        private void InitializeGallery()
        {
            if (gallery == null)
            {
                return;
            }
            gallery.ImageFetching += new EventHandler<SlideFetchProgressEventArgs>(ImageFetching);
            gallery.ImageFetched += new EventHandler<SlideFetchEventArgs>(ImageFetched);
            gallery.ItemAdded += new EventHandler<OverridableList<FetchableSlide>.ItemEventArgs>(SlideAdded);
            gallery.ItemRemoved += new EventHandler<OverridableList<FetchableSlide>.ItemEventArgs>(SlideRemoved);
        }

        private void FinalizeGallery()
        {
            if (gallery == null)
            {
                return;
            }
            gallery.ImageFetching -= new EventHandler<SlideFetchProgressEventArgs>(ImageFetching);
            gallery.ImageFetched -= new EventHandler<SlideFetchEventArgs>(ImageFetched);
            gallery.ItemAdded -= new EventHandler<OverridableList<FetchableSlide>.ItemEventArgs>(SlideAdded);
            gallery.ItemRemoved -= new EventHandler<OverridableList<FetchableSlide>.ItemEventArgs>(SlideRemoved);
        }

        #region Event handlers

        private void SlideAdded(object sender, OverridableList<FetchableSlide>.ItemEventArgs e)
        {
            this.Dispatcher.BeginInvoke(delegate()
            {
                tbTitle.Text = string.Format("{0} - ({1}/{2}) {3}", gallery.Title, gallery.CurrentIndex + 1, gallery.Count, e.Item.Caption);
            });
        }

        private void SlideRemoved(object sender, OverridableList<FetchableSlide>.ItemEventArgs e)
        {
            this.Dispatcher.BeginInvoke(delegate()
            {
                tbTitle.Text = string.Format("{0} - ({1}/{2}) {3}", gallery.Title, gallery.CurrentIndex + 1, gallery.Count, e.Item.Caption);
            });
        }

        private void ImageFetching(object sender, SlideFetchProgressEventArgs e)
        {
            this.Dispatcher.BeginInvoke(delegate()
            {
                pb.Value = e.ProgressPercentage;
            });
        }

        private void ImageFetched(object sender, SlideFetchEventArgs e)
        {
            this.Dispatcher.BeginInvoke(delegate()
            {
                tbTitle.Text = string.Format("{0} - ({1}/{2}) {3}", gallery.Title, gallery.CurrentIndex + 1, gallery.Count, e.Slide.Caption);
            });
        }

        private void ImageButton_First_Click(object sender, RoutedEventArgs e)
        {
            gallery.FetchFirstSlide();
        }

        private void ImageButton_Prev_Click(object sender, RoutedEventArgs e)
        {
            gallery.FetchPreviousSlide();
        }

        private void ImageButton_Pause_Click(object sender, RoutedEventArgs e)
        {
            IsAutoAdvanceActive = false;
        }

        private void ImageButton_Play_Click(object sender, RoutedEventArgs e)
        {
            IsAutoAdvanceActive = true;
        }

        private void ImageButton_Next_Click(object sender, RoutedEventArgs e)
        {
            gallery.FetchNextSlide();
        }

        private void ImageButton_Last_Click(object sender, RoutedEventArgs e)
        {
            gallery.FetchLastSlide();
        }

        private void ImageButton_FullScreen_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Host.Content.IsFullScreen = isFullScreen = !isFullScreen;
        }

        private void ImageButton_Save_Click(object sender, RoutedEventArgs e)
        {
        }

        #endregion

        #endregion

    }

}